#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/select.h>
#include <signal.h>
#include <linux/input.h>
#define PATH "/dev/my_key"
 
int main(int argc, char* argv[])
{
    int len, ret;
    struct input_event inputevent = {0};
    int fd, push_cnt = 0, release_cnt = 0;
    char* path = argv[1];
    char push[] = "push";
    char release[] = "release";
    if (argc != 2) {
        printf("path error\n");
        return -1;
    }
    fd = open(path, O_RDONLY); // 阻塞打开
    if (fd < 0){
        printf("my_key open error\n");
        return -1;
    }
    while (1) {
        ret = read(fd, &inputevent, sizeof(inputevent));
        if (ret > 0) {
            switch (inputevent.type)
            {
            case EV_KEY:
                printf("key %d %s %d\n",inputevent.code, 
                inputevent.value ? push : release, 
                inputevent.value ? push_cnt : release_cnt);
                inputevent.value ? push_cnt++ : release_cnt++;
                break;
            default:
                break;
            }
        } else printf("read error\n");
    }
    close(fd);
    return 0;
}
